package com.radiant.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.google.inject.Inject;
import com.radiant.data.entity.Comment;
import com.radiant.data.entity.CommentDAO;
import com.radiant.data.entity.CommentLike;
import com.radiant.data.entity.Like;
import com.radiant.data.entity.Post;
import com.radiant.data.entity.PostDAO;
import com.radiant.data.entity.PostLike;
import com.radiant.data.entity.UserDAO;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/post")
public class PostServiceImpl implements PostService {

	@Inject
	UserService userService;

	@Inject
	PhotoService photoService;

	@Inject
	ActivityFeedService feedService;

	@Inject
	PostDAO postDAO;
	
	@Inject
	CommentDAO commentDAO;

	@Inject
	UserDAO userDAO;

	public PostServiceImpl() {
		super();
	}
	
	public List<String> getProtectedPaths() {
		List<String> paths = new ArrayList<String>();
		UriBuilder builder = UriBuilder.fromResource(this.getClass());
		paths.add(builder.path("*").build().toString());
		return paths;
	}

	@POST @Path("/text/new")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public Post createTextPost(@FormParam("message") String message) {
		String from = userService.getCurrentUser().getUserName();
		Post post = new Post();
		post.setFrom(from);
		post.setMessage(message);
		post.setTimeCreated(new Date());
		post.setTimeUpdated(new Date());
		post = postDAO.save(post);
		feedService.generateActivityItems(userDAO.findByUserName(from), post);
		return post;
	}

	@POST @Path("/image/new")
	@Consumes(MediaType.MULTIPART_FORM_DATA)  
	@Produces("application/json")
	public Post createImagePost(@FormDataParam("message") String message,
			@FormDataParam("file") InputStream uploadedInputStream,
			@FormDataParam("file") FormDataContentDisposition fileDetail) {
		String from = userService.getCurrentUser().getUserName();
		Post post = new Post();
		post.setFrom(from);
		post.setMessage(message);
		post.setTimeCreated(new Date());
		post.setTimeUpdated(new Date());
		post.setPicture(photoService.storePhoto(uploadedInputStream));
	
		post = postDAO.save(post);

		feedService.generateActivityItems(userDAO.findByUserName(from), post);
		return post;
	}

	@POST @Path("/delete")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public void deletePost(@FormParam("postId") String postId) {
		String from = userService.getCurrentUser().getUserName();
		Post post = postDAO.findByPrimaryKey(postId, true);
		postDAO.delete(post);
	}

	@POST @Path("/comment/new")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public Comment addComment(@FormParam("postId") String postId, 
			@FormParam("message") String message) {
		String from = userService.getCurrentUser().getUserName();
		Post post = postDAO.findByPrimaryKey(postId, true);
		Comment comment = new Comment(true);
		comment.setFrom(from);
		comment.setMessage(message);
		comment.setTimeCreated(new Date());
		post.getComments().add(comment);
		
		postDAO.save(post);
		
		feedService.generateActivityItems(userDAO.findByUserName(from), comment);
		return comment;
	}

	@POST @Path("/likePost")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public Like likePost(@FormParam("postId") String postId) {
		String from = userService.getCurrentUser().getUserName();
		Post post = postDAO.findByPrimaryKey(postId, true);
		Like like = new PostLike(post);
		like.setTimeCreated(new Date());
		like.setFrom(from);
		post.getLikes().add(like);

		postDAO.save(post);
		
		feedService.generateActivityItems(userDAO.findByUserName(from), like);
		return like;
	}

	@POST @Path("/likeComment")
	@Consumes("application/x-www-form-urlencoded")
	@Produces("application/json")
	public Like likeComment(@FormParam("commentId") String commentId) {
		String from = userService.getCurrentUser().getUserName();
		Comment comment = commentDAO.findByPrimaryKey(commentId, true);
		Like like = new CommentLike(comment);
		like.setTimeCreated(new Date());
		like.setFrom(from);
		comment.getLikes().add(like);
	
		commentDAO.save(comment);

		feedService.generateActivityItems(userDAO.findByUserName(from), like);
		return like;
	}

	@GET @Path("/topN")
	@Produces("application/json")
	public List<Post> getTopNPosts(@QueryParam("count")int count) {
		String from = userService.getCurrentUser().getUserName();
		return postDAO.getTopNPosts(from, count);
	}

	@GET @Path("/timeInterval")
	@Produces("application/json")
	public List<Post> getPostsInTimeInterval(@QueryParam("startTimeInMillis") long startTimeInMillis, 
			@QueryParam("endTimeInMillis") long endTimeInMillis) {
		String from = userService.getCurrentUser().getUserName();
		return postDAO.getPostsInTimeInterval(from, startTimeInMillis, endTimeInMillis);
	}

	@GET @Path("/recent")
	@Produces("application/json")
	public List<Post> getRecentPosts(@QueryParam("timeInMillis") long timeInMillis) {
		String from = userService.getCurrentUser().getUserName();
		return postDAO.getRecentPosts(from, timeInMillis);
	}

}
